<route lang="yaml">
meta:
  whiteList: true
  title: 登录
  constant: true
  layout: false
    </route>

<script lang="ts" setup name="LoginRegister">
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { ikStore } from 'iking-utils'
import { useI18n } from 'vue-i18n'

const emit = defineEmits(['update:formType'])
const { t } = useI18n()

const loading = ref(false)

// 注册
const registerFormRef = ref<FormInstance>()
const registerForm = ref({
  phone: '',
  captcha: ''
})
const registerRules = ref<FormRules>({
  phone: [
    { required: true, trigger: 'blur', message: '请输入手机号' }
  ],
  captcha: [
    { required: true, trigger: 'blur', message: '请输入验证码' }
  ]
})
function handleLogin() {
  ElMessage({
    message: '只提供页面,短信登录请自行扩展',
    type: 'warning'
  })
  registerFormRef.value && registerFormRef.value.validate(valid => {
    if (valid) {
      // 这里编写业务代码
    }
  })
}

const timeOver = ref(0)
let timeInter: number | null
const name = 'iking-timer'

const setTimer = () => {
  timeInter = window.setInterval(() => {
    timeOver.value--
    ikStore.local.setItem(name, timeOver.value)
    if (timeOver.value === 0) {
      timeInter && clearInterval(timeInter)
      timeInter = null
      ikStore.local.removeItem(name)
    }
  }, 1000)
}

const handSendMsg = async () => {
  timeOver.value = 60
  setTimer()
}

onMounted(() => {
  if (ikStore.local.has(name)) {
    timeOver.value = ikStore.local.getItem(name)
    if (!timeOver.value)
      return
    setTimer()
  }
})
</script>

<template>
  <el-form
    ref="registerFormRef"
    :model="registerForm"
    :rules="registerRules"
    class="login-form"
    auto-complete="on"
  >
    <div class="title-container">
      <h3 class="login-title">
        {{ t('app.phoneLogin') }}
      </h3>
    </div>
    <div>
      <el-form-item prop="phone">
        <el-input
          v-model.trim="registerForm.phone"
          :placeholder="t('app.phone')"
          type="text"
          maxlength="11"
          tabindex="1"
        >
          <template #prefix>
            <el-icon>
              <IkSvgIcon name="i-ri:user-3-fill" />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="captcha">
        <el-input v-model.trim="registerForm.captcha" :placeholder="t('app.code')" tabindex="2">
          <template #prefix>
            <el-icon>
              <IkSvgIcon name="i-ic:baseline-verified-user" />
            </el-icon>
          </template>
          <template #append>
            <el-button :disabled="timeOver !== 0" @click="handSendMsg">
              {{ !timeOver ? `${t('action.send')}${t('app.code')}` : `${timeOver}${t('validate.afterSecond')},${t('action.resend')}` }}
            </el-button>
          </template>
        </el-input>
      </el-form-item>
    </div>
    <el-button
      :loading="loading"
      type="primary"
      size="large"
      style="width: 100%; margin-top: 20px;"
      @click.prevent="handleLogin"
    >
      {{ t('app.login') }}
    </el-button>
    <div class="sub-link">
      <el-link type="primary" :underline="false" @click="emit('update:formType', 'login')">
        {{ t('app.password') }}{{ t('app.login') }}
      </el-link>
    </div>
  </el-form>
</template>

  <style lang="scss" scoped>
  @import './form';
  </style>
